<template>
  <div class="mui-numbox" data-numbox-min='1' :data-numbox-max="max" :style="myStyle">
    <button class="mui-btn mui-numbox-btn-minus" type="button">-</button>
    <input class="mui-numbox-input" type="number" :value="initCount" ref="num" @change="countChange" />
    <button class="mui-btn mui-numbox-btn-plus" type="button">+</button>
  </div>
</template>

<script>
import mui from '../lib/mui/js/mui.min.js'

export default {
  props: ['max', 'initCount', 'goodsid', 'size'],
  data() {
    return {
      myStyle: {}
    }
  },
  created() {
    
  },
  mounted() {
    // 动态添加的Numbox组件需要手动初始化
    mui('.mui-numbox').numbox()
    if (this.$props.size === 'min') {
      this.myStyle = {
        width: '110px',
        height: '25px',
        margin: '5px 10px'
      }
    }
  },
  methods: {
    countChange() {
      // 将数字输入框中的数量返回给GoodsInfo页面
      const count = parseInt(this.$refs.num.value)
      this.$emit('count', {count, id: this.goodsid})
    }
  },
  watch: {
    // 库存可能会改变，需要实时更新最大值
    max(newVal) {
      mui('.mui-numbox').numbox().setOption('max', newVal)
    }
  }
}
</script>

<style>

</style>